home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / pine3.96.tar.gz / pine3.96.tar / pine3.96 / imap / ANSI / c-client / sm_mac.c < prev    next >
C/C++ Source or Header  |  1993-12-13  |  5KB  |  159 lines

  1. /*
  2.  * Program:    Subscription Manager (Mac based)
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        6158 Lariat Loop NE
  6.  *        Bainbridge Island, WA  98110-2098
  7.  *        Internet: MRC@Panda.COM
  8.  *
  9.  * Date:    13 December 1993
  10.  * Last Edited:    13 December 1993
  11.  *
  12.  * Copyright 1993 by Mark Crispin
  13.  *
  14.  *  Permission to use, copy, modify, and distribute this software and its
  15.  * documentation for any purpose and without fee is hereby granted, provided
  16.  * that the above copyright notice appears in all copies and that both the
  17.  * above copyright notices and this permission notice appear in supporting
  18.  * documentation, and that the name of Mark Crispin not be used in advertising
  19.  * or publicity pertaining to distribution of the software without specific,
  20.  * written prior permission.  This software is made available "as is", and
  21.  * MARK CRISPIN DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO
  22.  * THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED WARRANTIES OF
  23.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN NO EVENT SHALL
  24.  * MARK CRISPIN BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES
  25.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  26.  * WHETHER IN AN ACTION OF CONTRACT, TORT (INCLUDING NEGLIGENCE) OR STRICT
  27.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  28.  * THIS SOFTWARE.
  29.  *
  30.  */
  31.  
  32.  
  33. #include "mail.h"
  34. #include "osdep.h"
  35. #include "misc.h"
  36. #include <unix.h>
  37. #include <fcntl.h>
  38.  
  39. #define SUBSCRIPTIONFILE(t) strcpy (t,"Mailbox List")
  40.  
  41. /* Subscribe to mailbox
  42.  * Accepts: mailbox name
  43.  * Returns: T on success, NIL on failure
  44.  */
  45.  
  46. long sm_subscribe (char *mailbox)
  47. {
  48.   int fd;
  49.   char *s,*t,*txt,tmp[MAILTMPLEN],tmpx[MAILTMPLEN];
  50.   struct stat sbuf;
  51.   long ret = T;
  52.   SUBSCRIPTIONFILE (tmp);    /* open subscription database */
  53.   if ((fd = open (tmp,O_RDWR|O_CREAT|O_BINARY)) >= 0) {
  54.     fstat (fd,&sbuf);        /* get file size and read data */
  55.     read (fd,s = txt = (char *) fs_get (sbuf.st_size + 1),sbuf.st_size);
  56.     s[sbuf.st_size] = '\0';    /* tie off string */
  57.     while (t = strchr (s,'\015')){/* for each line in database */
  58.       *t++ = '\0';        /* tie off string */
  59.       if (strcmp (s,mailbox)) s = t;
  60.       else {            /* oops, it's subscribed */
  61.     sprintf (tmp,"Already subscribed to mailbox %s",mailbox);
  62.     mm_log (tmp,ERROR);
  63.     ret = NIL;
  64.     break;
  65.       }
  66.     }
  67.     if (ret) {            /* append to end of file if OK */
  68.       lseek (fd,sbuf.st_size,SEEK_SET);
  69.       sprintf (tmp,"%s\015",mailbox);
  70.       write (fd,tmp,strlen (tmp));
  71.     }
  72.     close (fd);            /* close off file */
  73.     fs_give ((void **) &txt);    /* free database */
  74.   }
  75.   else {
  76.     mm_log ("Can't create subscription database",ERROR);
  77.     ret = NIL;
  78.   }
  79.   return ret;            /* all done */
  80. }
  81.  
  82.  
  83. /* Unsubscribe from mailbox
  84.  * Accepts: mailbox name
  85.  * Returns: T on success, NIL on failure
  86.  */
  87.  
  88. long sm_unsubscribe (char *mailbox)
  89. {
  90.   int fd;
  91.   char *s,*t,*txt,*end,tmp[MAILTMPLEN],tmpx[MAILTMPLEN];
  92.   struct stat sbuf;
  93.   long ret = NIL;
  94.   SUBSCRIPTIONFILE (tmp);    /* open subscription database */
  95.   if ((fd = open (tmp,O_RDONLY|O_BINARY)) >= 0) {
  96.     fstat (fd,&sbuf);        /* get file size and read data */
  97.     read (fd,s = txt = (char *) fs_get (sbuf.st_size + 1),sbuf.st_size);
  98.     close (fd);
  99.                 /* tie off string */
  100.     *(end = s + sbuf.st_size) = '\0';
  101.     while (t = strchr (s,'\015')){/* for each line in database */
  102.       *t++ = '\0';        /* temporarily tie off string */
  103.       if (strcmp (s,mailbox)) {    /* match? */
  104.     t[-1] = '\n';        /* no, restore newline */
  105.     s = t;            /* try next subscription */
  106.       }
  107.       else {            /* cancel subscription */
  108.     if (t != end) memcpy (s,t,1 + end - t);
  109.     end -= t - s;        /* calculate new end of database */
  110.     ret = T;        /* note cancelled a subscription */
  111.       }
  112.     }
  113.     if (ret) {            /* found any? */
  114.       if ((fd = open (tmp,O_WRONLY|O_TRUNC|O_BINARY)) >= 0) {
  115.     if (end != txt) write (fd,txt,end - txt);
  116.     close (fd);        /* close off file */
  117.       }
  118.       else mm_log ("Error re-opening subscription file",ERROR); 
  119.     }
  120.     else {            /* subscription not found */
  121.       sprintf (tmp,"Not subscribed to mailbox %s",mailbox);
  122.       mm_log (tmp,ERROR);    /* error if at end */
  123.     }
  124.     fs_give ((void **) &txt);    /* free database */
  125.   }
  126.   else {
  127.     mm_log ("No subscriptions",ERROR);
  128.     ret = NIL;
  129.   }
  130.   return ret;            /* all done */
  131. }
  132.  
  133.  
  134. /* Read subscription database
  135.  * Accepts: pointer to subscription database handle (handle NIL if first time)
  136.  * Returns: character string for subscription database or NIL if done
  137.  */
  138.  
  139. char *sm_read (void **sdb)
  140. {
  141.   int fd;
  142.   char *s,*t,tmp[MAILTMPLEN];
  143.   struct stat sbuf;
  144.   if (!*sdb) {            /* first time through? */
  145.     SUBSCRIPTIONFILE (tmp);    /* open subscription database */
  146.     if ((fd = open (tmp,O_RDONLY|O_BINARY)) >= 0) {
  147.       fstat (fd,&sbuf);        /* get file size and read data */
  148.       read (fd,s = (char *) (*sdb = fs_get (sbuf.st_size + 1)),sbuf.st_size);
  149.       close (fd);        /* close file */
  150.       s[sbuf.st_size] = '\0';    /* tie off string */
  151.       if (t = strtok (s,"\015")) return t;
  152.     }
  153.   }
  154.                 /* subsequent times through database */
  155.   else if (t = strtok (NIL,"\015")) return t;
  156.   fs_give (sdb);        /* free database */
  157.   return NIL;            /* all done */
  158. }
  159.